<template>
    <!-- 表单部分 -->
    <el-form inline>
      <el-form-item label="名称：">
        <el-input v-model="queryForm.name" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getSpecPage">
          <el-icon><Search /></el-icon>搜索
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addSpec">+新增规格</el-button>
        <el-button type="danger" @click="deleBatch">批量删除</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格展示部分 -->
    <el-table
      ref="tableRef"
      v-loading="loading"
      :data="specList"
      style="width: 100%"
    >
      <!-- 表格列定义 -->
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="name" label="规格名称" width="200" />
      <el-table-column label="规格值">
        <template #default="{ row }">
          <div style="display: flex; gap: 10px">
            <el-tag v-for="specValue in row.specValueList" :key="specValue.id" type="success">{{ specValue.name }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-popconfirm
            title="确认删除吗?"
            confirm-button-text="确认"
            cancel-button-text="取消"
            @confirm="deleSpec(row.id)"
          >
            <template #reference>
              <el-button type="danger">
                <el-icon><Delete /></el-icon>
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>

    <!-- 分页部分 -->
    <el-pagination
      v-model:current-page="queryForm.page"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[2, 5, 10, 20]"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="getSpecPage"
      @current-change="getSpecPage"
    />

    <!-- 规格编辑对话框（组件） -->
    <SpecEdit ref="dialog" @success="onSuccess" />
</template>

<script setup>
// 引入依赖
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { getSpecPageAPI, deleSpecAPI } from "@/apis/goods.js";
import SpecEdit from "./components/SpecEdit.vue";

// 定义响应式数据
const queryForm = ref({
  page: 1,
  pageSize: 10,
  name: "",
});

const specList = ref([]);
const loading = ref(false);
const total = ref(0);
const dialog = ref(null);
const tableRef = ref(null);

// 方法定义区域
const getSpecPage = async () => {
  loading.value = true;
  const res = await getSpecPageAPI(queryForm.value);
  specList.value = res.data.records;
  total.value = res.data.total;
  loading.value = false;
};

const addSpec = () => {
  dialog.value.open();
};

const deleSpec = async (id) => {
  const res = await deleSpecAPI(id);
  if (res.code === 1) {
    ElMessage.success("删除成功");
    getSpecPage();
  }
};

const deleBatch = () => {
  const selectedRows = tableRef.value.getSelectionRows();
  if (selectedRows.length === 0) {
    ElMessage.warning("没有选中的规格");
  } else {
    const selectedIds = selectedRows.map((row) => row.id).join(",");
    deleSpec(selectedIds);
  }
};

const onSuccess = async () => {
  const res = await getSpecPageAPI(queryForm.value);
  const totalPages = Math.ceil(res.data.total / queryForm.value.pageSize);
  queryForm.value.page = totalPages;
  getSpecPage();
};

// 生命周期钩子
onMounted(() => {
  getSpecPage();
});
</script>